/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigsquickstart.repository.jpa.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.synyx.hades.dao.GenericDao;
import org.synyx.hades.domain.AbstractPersistable;
import org.synyx.hades.domain.Order;
import org.synyx.hades.domain.PageRequest;

import com.googlecode.chigscommons.domain.User;
import com.googlecode.chigscommons.repository.GenericDomainService;
import com.googlecode.chigscommons.repository.UncheckedRuntimeException;
import com.googlecode.chigscommons.repository.jpa.UserDao;
import com.googlecode.chigsquickstart.domain.HelloWorld;
import com.googlecode.chigsquickstart.repository.jpa.HelloWorldDao;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Service("genericDomainService")
@Transactional(readOnly = true)
public class GenericDomainServiceImpl implements GenericDomainService {
	@Resource
	private UserDao userDao;

	@Resource
	private HelloWorldDao helloWorldDao;

	@SuppressWarnings("unchecked")
	private <T extends AbstractPersistable<Long>> GenericDao<T, Long> getDao(Class<T> type) {
		if (type == User.class) {
			return (GenericDao<T, Long>) userDao;
		} else if (type == HelloWorld.class) {
			return (GenericDao<T, Long>) helloWorldDao;
		}

		throw new UncheckedRuntimeException("no dao found for type: " + type.getName());
	}

	@Override
	public <T extends AbstractPersistable<Long>> T load(Class<T> type, Long id) {
		return getDao(type).readByPrimaryKey(id);
	}

	@Override
	public <T extends AbstractPersistable<Long>> List<T> loadAll(Class<T> type, final int from,
			final int count, final String sortProperty, final boolean sortAsc) {
		final PageRequest pageable;
		
		int page;
		
		if (from > 0) {
			page = from / count;
		} else {
			page = 0;
		}
		
		if (sortProperty == null || "".equals(sortProperty.trim())) {
			pageable = new PageRequest(page , count);
		} else {
			pageable = new PageRequest(page, count, sortAsc ? Order.ASCENDING : Order.DESCENDING, sortProperty);
		}
		return getDao(type).readAll(pageable).asList();
	}

	@Override
	public <T extends AbstractPersistable<Long>> List<T> loadAll(Class<T> type) {
		return getDao(type).readAll();
	}

	@Override
	public <T extends AbstractPersistable<Long>> int getCount(Class<T> type) {
		return getDao(type).count().intValue();
	}

	@Transactional(readOnly = false)
	@Override
	public <T extends AbstractPersistable<Long>> void save(Class<T> type, T object) {
		getDao(type).save(object);
	}

	@Transactional(readOnly = false)
	@Override
	public <T extends AbstractPersistable<Long>> void delete(Class<T> type, T object) {
		getDao(type).delete(getDao(type).readByPrimaryKey(object.getId()));
	}
}
